package handin03;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

/**
 * HoroscopeTextComponent is a JComponent for the texts
 * @author Peter Tolstrup Aagesen
 *
 */

@SuppressWarnings("serial")
public class HoroscopeTextComponent extends JComponent{	
	
	//Margin
	private static final int MARGIN = 18;

	//Font styling
	private Font headlineFont = new Font("sans", 18, 18);
	private Font weekFont = new Font("sans", 11, 11);
	private Font maintextFont = new Font("sans", 12, 12);
	
	//Variables for displaying the 
	private final int MAINTEXTLEADING = 14;
	private final int MAINTEXTWIDTH = 60;
	
	//Variables for the text
	private String headline= "";
	private String week = "";
	private String maintext = "";
	
	public HoroscopeTextComponent(String signInfo, String weekText, String horoscopeText)
	{
		//Set instance variables
		headline = signInfo;
		week = weekText;
		maintext = horoscopeText;
	}

	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		
		//Set font and draw headline
		g2.setFont(headlineFont);		
		g2.drawString(headline, MARGIN, MARGIN * 2);

		//Set font and draw week text
		g2.setFont(weekFont);	
		g2.drawString(week, MARGIN, MARGIN * 3);

		//Set font
		g2.setFont(maintextFont);
		
		//Variables for drawing the main text in chunks of MAINTEXTWIDTH  
		int textStartPosition = 0;
		int textEndPosition = MAINTEXTWIDTH;
		//Variable for changing the y positing
		int lineCounter = 0;
		
		while (textEndPosition < maintext.length()) //While the end position is smaller than the length of the text
		{
			//Check if there's a white space 
			boolean isWhiteSpace = false;
			while(!isWhiteSpace) //While there's not a whitespace at the before the end position
			{
				//Get the char before the end position
				char testChar = maintext.charAt(textEndPosition - 1); 
				if (testChar == ' '){ //If there's a whitespace 
					isWhiteSpace = true; //Set isWhiteSpace to true => end while
				} else {
					textEndPosition--; //Else check the char before
				}
			}				
			//Draw chunk of text
			g2.drawString(maintext.substring(textStartPosition, textEndPosition), MARGIN, MARGIN * 4 + MAINTEXTLEADING * lineCounter);
			//Set new positions
			textStartPosition = textEndPosition;
			textEndPosition += MAINTEXTWIDTH;
			//Add one to line
			lineCounter ++;
			
		}
		//Set the end position to length of text
		textEndPosition = maintext.length();
		//Draw last text
		g2.drawString(maintext.substring(textStartPosition, textEndPosition), MARGIN, MARGIN * 4 + MAINTEXTLEADING * lineCounter);
	}
	
}
